package com.shfl.servermanage.logic.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.shfl.servermanage.exception.ApiException;
import com.shfl.servermanage.logic.bean.SysServerBt;
import com.shfl.servermanage.logic.bean.SysServerShell;
import com.shfl.servermanage.logic.mapper.SysServerShellMapper;
import com.shfl.servermanage.logic.model.request.EditServerShellRequest;
import com.shfl.servermanage.logic.model.response.ServerShellDetailResponse;
import com.shfl.servermanage.logic.service.SysServerShellService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class SysServerShellServiceImpl implements SysServerShellService {
    @Autowired
    private SysServerShellMapper sysServerShellMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertSelective(SysServerShell record) {
        return sysServerShellMapper.insertSelective(record);
    }

    @Override
    public void findByServerId(ServerShellDetailResponse serverShellDetailResponse, Long serverId) {
        Map<String, Object> objectMap = new HashMap<>();
        objectMap.put("serverId", serverId);
        List<SysServerShell> sysServerShells = sysServerShellMapper.findListByMap(objectMap);
        if (!CollectionUtils.isEmpty(sysServerShells)) {
            SysServerShell sysServerShell = sysServerShells.get(0);
            BeanUtil.copyProperties(sysServerShell, serverShellDetailResponse);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void edit(EditServerShellRequest editServerShellRequest) {
        SysServerShell sysServerShell = sysServerShellMapper.selectByPrimaryKey(editServerShellRequest.getId());
        if (sysServerShell == null || !Objects.equals(sysServerShell.getServiceId(), editServerShellRequest.getServiceId())) {
            throw new ApiException("1204");
        }
        SysServerShell updateSysServerShell = new SysServerShell();
        BeanUtil.copyProperties(editServerShellRequest, updateSysServerShell);
        sysServerShellMapper.updateByPrimaryKeySelective(updateSysServerShell);
    }

    @Override
    public List<SysServerShell> findListByMap(Map<String, Object> objectMap) {
        return sysServerShellMapper.findListByMap(objectMap);
    }
}
